package com.cy.cuiyang_admin.util;

import com.cy.cuiyang_admin.controller.EmpController;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

@Slf4j
public class JsonHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonHelper.class);

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private static final ObjectMapper NOT_NULL_MAPPER = new ObjectMapper();

    private final static String ISO_DATE_HOUR24_MIN_SEC = "yyyy-MM-dd HH:mm:ss";

    static
    {
        MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //MAPPER.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MAPPER.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        MAPPER.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        MAPPER.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        //MAPPER.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);

        DateFormat df = new SimpleDateFormat(ISO_DATE_HOUR24_MIN_SEC);
        MAPPER.setDateFormat(df);


        NOT_NULL_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        NOT_NULL_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        NOT_NULL_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        NOT_NULL_MAPPER.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        NOT_NULL_MAPPER.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        NOT_NULL_MAPPER.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        //MAPPER.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);

        DateFormat df2 = new SimpleDateFormat(ISO_DATE_HOUR24_MIN_SEC);
        NOT_NULL_MAPPER.setDateFormat(df2);

    }

    public static String toJson(Object model) {
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            MAPPER.writeValue(output, model);
            return output.toString("UTF-8");
        } catch (JsonProcessingException e) {
            LOGGER.error("异常!",e.getMessage());
        } catch (IOException e) {
            LOGGER.error("异常!",e.getMessage());
        }

        return null;
    }

    public static byte[] toJsonBytes(Object model) {
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            MAPPER.writeValue(output, model);

            return output.toByteArray();
        } catch (JsonProcessingException e) {
            LOGGER.error("异常!",e.getMessage());
        } catch (IOException e) {
            LOGGER.error("异常!",e.getMessage());
        }

        return null;
    }

    public static OutputStream toJson(Object model, OutputStream outputStream) {
        try {
            MAPPER.writeValue(outputStream, model);
            return outputStream;
        } catch (JsonProcessingException e) {
            LOGGER.error("异常!",e.getMessage());
        } catch (IOException e) {
            LOGGER.error("异常!",e.getMessage());
        }

        return null;
    }

    public static Object toObject(String json, Class<?> modelType) {
        try {
            Object model = MAPPER.readValue(json, modelType);

            return model;
        } catch (JsonProcessingException e) {
            LOGGER.error("异常!",e.getMessage());
        } catch (IOException e) {
            LOGGER.error("异常!",e.getMessage());
        }

        return null;
    }

    public static Object toObject(byte[] json, Class<?> modelType) {
        try {
            return MAPPER.readValue(json, modelType);
        } catch (JsonProcessingException e) {
            LOGGER.error("异常!",e.getMessage());
        } catch (IOException e) {
            LOGGER.error("异常!",e.getMessage());
        }

        return null;
    }


    public static Object toObject(InputStream jsonStream, Class<?> modelType) {
        try {
            Object model = MAPPER.readValue(jsonStream, modelType);
            return model;
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("Parse Json Fail", e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Parse Json Fail", e);
        }
    }


}
